package com.by.course.entity.base;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Collections;
import java.util.List;

/**
 *
 * @author zhd
 * @date 2023/09/14 12:55
 **/
@Data
public class PageResult<T> {

    @ApiModelProperty("页码")
    private int pageNumber;

    @ApiModelProperty("每页展示数量")
    private int pageSize;

    @ApiModelProperty("数据")
    private List<T> list;

    @ApiModelProperty("查询数据总数")
    private int totalRow;

    @ApiModelProperty("总页数")
    private int totalPage;

    @ApiModelProperty("上一页页码")
    private int prevPage;

    @ApiModelProperty("下一页页码")
    private int nextPage;

    public static <T> PageResult<T> genEmptyData() {
        return new PageResult<>(Collections.emptyList(), 0, 1, 1);
    }

    public PageResult(List<T> list, int totalRow, int pageNumber, int pageSize) {
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
        this.list = list;
        this.totalRow = totalRow;

        //如果一页可以显示完,就直接指定值
        if (totalRow < pageSize) {
            this.totalPage = 1;
            this.prevPage = 1;
            this.nextPage = 1;
        }

        this.totalPage = totalRow % pageSize == 0 ? totalRow / pageSize : totalRow / pageSize + 1;
        this.prevPage = pageNumber - 1 > 0 ? pageNumber - 1 : 1;
        this.nextPage = Math.min(pageNumber + 1, totalPage);
    }
}

